
package com.epam.android.training.level1.activity;

import com.epam.android.training.level1.R;
import com.epam.android.training.level1.db.dao.RssDao;
import com.epam.android.training.level1.db.dao.impl.RssDaoImpl;
import com.epam.android.training.level1.rss.NewsFeed;
import com.epam.android.training.level1.widget.CollectionAdapter;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.net.URL;
import java.util.List;

/**
 * Activity responsible for RSS feeds reading. Feeds could be configured on
 * Settings page.
 * 
 * @author Anton Novikau
 */
public class NewsActivity extends BasePageActivity {

  private ListView feedContaier;

  private LayoutInflater inflater;

  /** Called when the activity is first created. */
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.news_titles);

    feedContaier = (ListView) findViewById(R.id.feed_container);
    inflater = getLayoutInflater();

    loadNews();
    addOpenFeedAction();
    addOpenNewsSiteAction();
  }

  private void loadNews() {
    RssDao rssDao = new RssDaoImpl(this);
    List<NewsFeed> rssFeeds = rssDao.findAllFeeds();
    if (rssFeeds == null || rssFeeds.isEmpty()) {
      Toast.makeText(this, R.string.feed_not_found, Toast.LENGTH_LONG).show();
    } else {
      feedContaier.setAdapter(new FeedAdapter(rssFeeds));
    }
  }

  private void addOpenFeedAction() {
    feedContaier.setOnItemClickListener(new AdapterView.OnItemClickListener() {

      @Override
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(ACTION_SINGLE_FEED);
        intent.putExtra(FEED_ID_EXTRA, id);
        startActivity(intent);
      }
    });
  }

  private void addOpenNewsSiteAction() {
    feedContaier.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

      @Override
      public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        NewsFeed feed = (NewsFeed) parent.getItemAtPosition(position);
        URL newsSiteLink = feed.getLink();
        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(newsSiteLink.toString()));
        startActivity(browserIntent);
        return true;
      }
    });
  }

  public class FeedAdapter extends CollectionAdapter<NewsFeed> {

    public FeedAdapter(List<NewsFeed> feeds) {
      super(feeds);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      ItemViewHolder holder;
      if (convertView == null) {
        convertView = inflater.inflate(R.layout.feed_title_item, parent, false);
        holder = new ItemViewHolder();
        holder.title = (TextView) convertView.findViewById(R.id.feed_title);
        holder.categories = (TextView) convertView.findViewById(R.id.feed_categories);

        convertView.setTag(holder);
      } else {
        holder = (ItemViewHolder) convertView.getTag();
      }
      NewsFeed feed = getItem(position);
      holder.title.setText(feed.getTitle());
      holder.categories.setText(buildCategories(feed.getCategories()));

      return convertView;
    }

    private CharSequence buildCategories(List<String> categories) {
      StringBuilder builder = new StringBuilder();
      if (!categories.isEmpty()) {
        builder.append("(").append(TextUtils.join(", ", categories)).append(")");
      }
      return builder;
    }
  }

  private class ItemViewHolder {
    private TextView title;

    private TextView categories;
  }
}
